package bigoo.peer.message;

import bigoo.common.communication.AbstractMessage;
import bigoo.common.communication.InvalidMessageException;
import bigoo.common.communication.Message;
import bigoo.common.communication.MessageBuilder;

public class RideRequestMessage extends AbstractMessage {

    public static final String MSG_IDENTIFIER = "RIDE_REQUEST";
    private String rideID;
    private int senderPort;
    private String senderAddress;

    public RideRequestMessage(String senderID, String senderAddress, int senderPort, String rideID) {
	super(senderID, MSG_IDENTIFIER);
	this.senderAddress = senderAddress;
	this.senderPort = senderPort;
	this.rideID = rideID;
    }

    public RideRequestMessage(String peerID, String senderAddress, String senderPort, String rideID) {
	this(peerID, senderAddress, Integer.parseInt(senderPort), rideID);
    }

    public String getRideID() {
	return this.rideID;
    }

    public int getSenderPort() {
	return this.senderPort;
    }

    public String getSenderAddress() {
	return this.senderAddress;
    }

    @Override
    public String getRawMessage() {
	return this.getSenderID() + SEPARATOR + MSG_IDENTIFIER + SEPARATOR + senderAddress + 
		PARAM_SEPARATOR + senderPort + PARAM_SEPARATOR + rideID + END_OF_MESSAGE;
    }

    public static class Builder implements MessageBuilder {
	@Override
	public Message parseMessage(String rawMessage) throws InvalidMessageException {
	    String senderID = parseSenderID(rawMessage);
	    String[] params = parseListParams(rawMessage);
	    String[] param = parseParams(params[0]);
	    return new RideRequestMessage(senderID, param[0], Integer.parseInt(param[1]), param[2]);
	}
    }
}
